<?php

namespace inhere\simditor;

use Yii;
use yii\web\AssetBundle;

/**
 * @link https://github.com/mycolorway/simditor
 */
class SimditorAsset extends AssetBundle
{
    public $sourcePath = '@bower';

    public $css = [
        'simditor/styles/simditor.css',
    ];

    public $js = [
		'simple-module/lib/module.js',
		'simple-hotkeys/lib/hotkeys.js',
		'simple-uploader/lib/uploader.js',
		'simditor/lib/simditor.js',
    ];

    public $depends = [
    	'yii\web\JqueryAsset',
    ];

    /**
     * enable simditor extension
     * @example 
     * simditor-html @link https://github.com/huyinghuan/simditor-html
     * simditor-fullscreen @link https://github.com/huyinghuan/simditor-fullscreen
     * simditor-markdown 
     * @var array
     */
    public static $extendOptions = [
        // 'html' => [
        //     'css' => [],
        //     'js'  => [],
        // ],
        // 'fullscreen',
        // 'simditor-markdown',
        'markdown' => [
            'css' => [
                'simditor-markdown/styles/simditor-markdown.css'
            ],
            'js'  => [
                'to-markdown/dist/to-markdown.js',
                'marked/lib/marked.js',
                'simditor-markdown/lib/simditor-markdown.js'
            ],
        ],
    ];

    public function init()
    {
        parent::init();

        foreach (static::$extendOptions as $name => $options) {
            if ( isset($options['css']) ) {
                $this->css = array_merge($this->css, (array)$options['css']);
            }
            
            if ( isset($options['js']) ) {
                $this->js = array_merge($this->js, (array)$options['js']);
            }
        }
    }

    public static function enableExtend(array $extends)
    {
        foreach (static::$extendOptions as $name => $options) {
            if ( !in_array($name, $extends) ) {
                unset(static::$extendOptions[$name]);
            }
        }

        return static::$extendOptions;
    }

}